import sys, os
sys.path.append(".")

from EasyHadoop import EasyHadoop
from utils.OptionParserUtil import *

"""
@author: ragrawal
@version: 1.0
@since: Mar 12, 2011
@summary: TODO 
"""

class ColExtractor(EasyHadoop):
    """
    Extracts specified columns from the input dataset. Requries at least one 
    column to be defined as key column
    """
    max_index = 0
    
    def __init__(self, arguments):
        """
        Constructor 
        
        @param arguments: input options either from command line or set while 
        initialization
        @type arguments: L{String} or L{List}
        """
        super(ColExtractor, self).__init__(arguments)
        
        #determining maximum column index value
        self.max_index = sorted(self.option['key']).pop(-1)
        if not self.option['value'] is None:
            tmp = sorted(self.option['value']).pop(-1)
            if tmp > self.max_index: max_index = tmp
        
        
    
    def set_mandatory(self):
        """set mandatory fields"""
        super(ColExtractor, self).set_mandatory()
        self.mandatory.append('--key')
        self.mandatory.append('--key-delimiter')
        self.mandatory.append('--value-delimiter')

        
    
    def set_commands(self):
        """Define custom options"""
        super(ColExtractor, self).set_commands()
        self.parser.add_option("--key", type="string", dest="key",
                               action="callback",callback=parse_int_array,
                               help="Specify key columns. Separate multiple,\
                               columns by comma", nargs = 1)
        self.parser.add_option("--key-delimiter",
                               action="callback", callback=handle_delimiter,
                               type="string", nargs=1, dest="key_delimiter",
                               help="delimiter to use in order to separate \
                               multiple key columns",default="|")
        self.parser.add_option("--value", type="string", dest="value",
                               action="callback",callback=parse_int_array,
                               help="Specify value columns. Separate multiple,\
                               columns by comma", nargs = 1)
        self.parser.add_option("--value-delimiter",
                               action="callback", callback=handle_delimiter,
                               type="string", nargs=1, dest="value_delimiter",
                               help="delimiter to use in order to separate \
                               multiple value columns",default="\t")

        
        
        
    def get_description(self):
        return "Extracts specified columns from the input dataset. \
        Requries at least one column to be defined as key column"
       
        #=================
        #TODO: return descrption
        #================
    
    def get_usage(self):
        """Usage information"""
        return "Usage: python %prog [options]"
    
    def execute(self, line):

        tokens = line.strip().split(self.options[:input_delimiter])
        
        #Sanity Check
        if tokens.length-1 < self.max_index: 
            if self.options['ignore']: return None
            raise Exception("Expecting atleast %d columns. Found %d" % (self.max_index+1, tokens.length))
        end
        
        #extract keys
        key = []
        for k in self.options['key']:
            key.append(tokens[k])
        key = self.options['key_delimiter'].join(key)
        
        #extract values
        value = []
        if not self.options['value'] is None:
            for v in self.options['value']: 
                value.append(tokens[v])
        value = self.options['value_delimiter'].join(value)
        
    
if __name__ == '__main__':
    cls = ColExtractor(sys.argv)
    
